package com.service.impl;

import com.POJO.Grade;
import com.POJO.GradeRequest;
import com.POJO.TeacherSubjectClass;
import com.mapper.GradeRequestMapper;
import com.mapper.GradeMapper;
import com.mapper.TeacherSubjectClassMapper;
import com.service.GradeRequestService;
import com.util.MybatisConfig;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package com.service
 * @Date 2024-05-24 12:03
 * @Description 成绩修改申请相关服务实现
 */
@Service
public class GradeRequestServiceImpl implements GradeRequestService {

    private GradeRequestMapper gradeRequestMapper;
    private GradeMapper gradeMapper;
    private TeacherSubjectClassMapper teacherSubjectClassMapper;
    private SqlSession sqlSession;


    // 初始化SqlSession和Mapper
    private void openSession(){
        sqlSession = MybatisConfig.getSession();
        gradeRequestMapper = sqlSession.getMapper(GradeRequestMapper.class);
        gradeMapper = sqlSession.getMapper(GradeMapper.class);
        teacherSubjectClassMapper = sqlSession.getMapper(TeacherSubjectClassMapper.class);
    }

    /**
     * 创建成绩修改申请
     * @param studentId 学生ID
     * @param subjectId 科目ID
     * @param reason 修改理由
     * @param requestedGrade 申请的成绩
     */
    @Override
    public void createGradeRequest(int studentId, int subjectId, String reason, double requestedGrade,String status) {
        openSession();
        GradeRequest gradeRequest = new GradeRequest(studentId, subjectId, reason, requestedGrade, "pending");
        gradeRequestMapper.insertGradeRequest(gradeRequest);
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据学生ID获取其所有成绩修改申请
     * @param studentId 学生ID
     * @return 成绩修改申请列表
     */
    @Override
    public List<GradeRequest> getGradeRequestsByStudentId(int studentId) {
        openSession();
        try {
            return gradeRequestMapper.getGradeRequestsByStudentId(studentId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 根据教师ID获取所有待审核的成绩修改申请
     * @param teacherId 教师ID
     * @return 待审核的成绩修改申请列表
     */
    @Override
    public List<GradeRequest> getPendingGradeRequestsByTeacher(int teacherId) {
        openSession();
        try {
            return gradeRequestMapper.getPendingGradeRequestsByTeacherId(teacherId);
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 批准成绩修改申请
     * @param requestId 申请ID
     */
    @Override
    public void approveRequest(int requestId) {
        openSession();
        GradeRequest gradeRequest = gradeRequestMapper.selectGradeRequestById(requestId);
        if (gradeRequest != null && "pending".equals(gradeRequest.getStatus())) {
            gradeRequest.setStatus("approved");
            gradeRequestMapper.updateGradeRequestStatus(gradeRequest);
            // 更新学生成绩
//            Grade grade = new Grade();
//            grade.setStudentId(gradeRequest.getStudentId());
//            grade.setSubjectId(gradeRequest.getSubjectId());
//            grade.setGrade(gradeRequest.getRequestedGrade());
            gradeMapper.updateGrade(gradeRequest.getRequestedGrade(), gradeRequest.getStudentId());
            sqlSession.commit();
            sqlSession.close();
        } else {
            sqlSession.close();
        }
    }

    /**
     * 拒绝成绩修改申请
     * @param requestId 申请ID
     */
    @Override
    public void rejectRequest(int requestId) {
        openSession();
        GradeRequest gradeRequest = gradeRequestMapper.selectGradeRequestById(requestId);
        if (gradeRequest != null && "pending".equals(gradeRequest.getStatus())) {
            gradeRequest.setStatus("rejected");
            gradeRequestMapper.updateGradeRequestStatus(gradeRequest);
        }
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据科目ID获取该科目的教师ID
     * @param subjectId 科目ID
     * @return 教师ID
     */
    @Override
    public int getTeacherIdBySubjectId(int subjectId) {
        openSession();
        List<TeacherSubjectClass> teacherSubjectClasses = teacherSubjectClassMapper.getTeacherSubjectClassBySubjectId(subjectId);
        if (!teacherSubjectClasses.isEmpty()) {
            try {
                return teacherSubjectClasses.get(0).getTeacherId();
            }catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                sqlSession.close();
            }
        } else {
            sqlSession.close();
        }
        return -1; // 或抛出一个异常，表示没有找到对应的教师
    }
}
